<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 * Add an opt group to a drop menu
 * @author Anthony Gallon
 * @package Antz
 */


/**
 * Add an opt group to a drop menu
 * @author Anthony Gallon
 * @package Antz
 */
class JFX_Form_Components_OptGroup extends JFX_Form_Component
{
    /**
     * Stack containing the options inside the opt-group
     * @var array $options
     */
    protected $options = array();
    /**
     * Define the values that are preselected in the drop menu
     * @var mixed $value
     */
    protected $value = array();

    /**
     * Set the initial values
     * @param string $title
     * @param mixed $value
     */
    public function __construct($title, $value=''){
        $this->title = $title;
        if(!is_array($value)) $value = array($value);
        $this->value = $value;
    }

    /**
     * Add an option to the opt-group
     * @param string $title
     * @param mixed $value
     * @return object JFX_Form_Components_Option
     */
    public function add($title, $value){
    	$selected = (in_array($value, $this->value)) ? true : false;
        if($value=='') $value = $title;
        $this->options[] = new JFX_Form_Components_Option($title, $value, $selected);
        return $this->options[(count($this->options)-1)];
    }

    /**
     * Get the element as an HTML string
     * @return string $html
     */
    public function render(){
        $return = '<optgroup label="'.$this->title.'">';
        foreach($this->options as $k=>$v){
            $return .= $v->render();
        };
        $return .= '</optgroup>';
        return $return;
    }
}
